<?php

_include(_OTEKOO_API_."APIDefault");
_include(_OTEKOO_MODEL_."Message");
_include(_OTEKOO_MODEL_."MessageMapper");
_include(_OTEKOO_MODEL_."User");
_include(_OTEKOO_MODEL_."UserMapper");

class APIMessage extends APIDefault
{
	private $_messageMapper = NULL;
	private $_userMapper = NULL;
	
	public function __construct(APIBootstrap $api){
		parent::__construct($api);
		$this->_messageMapper = new MessageMapper($this->_api->_db, $this->_api->_scripts);
		$this->_userMapper = new UserMapper($this->_api->_db, $this->_api->_scripts);
	}
	
	private function body(&$errors)
	{
		if(!isset($errors))
			$errors = array();
		$item = $this->_request->body;
		if(!isset($item) || empty($item) || strlen($item) < 20)
			$errors[] = "Invalid Message Body";
		return $item;
	}
	
	private function title(&$errors)
	{
		if(!isset($errors))
			$errors = array();
		$item = $this->_request->title;
		if(!isset($item) || empty($item) || strlen($item) < 10)
			$errors[] = "Invalid Message Title";
		return $item;
	}
	
	private function user(&$errors)
	{
		$item = $this->_userMapper->find($this->_request->token);
		if($item == null)
			$errors[] = "Missing user";
		return $item;
	}
	
	private function getTalkMessage(&$errors)
	{
		$message = new Message();
		$message->title = $this->title($errors);
		$message->body = $this->body($errors);
		$message->user = $this->user($errors);
		return $message;
	}
	
	private function talk_id(&$errors)
	{
		$item = $this->_request->talkid;
		if(!isset($item) || !is_numeric($item))
			$errors[] = "Missing Talk Id";
		return $item;
	}
	
	private function getReplyMessage(&$errors)
	{
		$message = new Message();
		$message->parent_id = $this->talk_id($errors);
		$message->body = $this->body($errors);
		$message->user = $this->_userMapper->find($this->_request->token);
		if($message->user == null)
			$errors[] = "Missing user";
		return $message;
	}
	
	/** 
	* Default method
	* @http_method post get put
	*/
	public function _default()
	{
		
	}
	
	/** 
	* Create new talk
	* @http_method post get put
	*/
	public function create()
	{
		$code = 202;
		$errors = array();
		try{
			$message = $this->getTalkMessage($errors);
			if(empty($errors))
			{
				$id = $this->_messageMapper->save($message);
			}
		}catch(Exception $e)
		{
			$errors[] = $e->getMessage();
		}
		if(!empty($errors))
		{
			$code = 400;
			$this->_response->errors = $errors;
		}
		$this->response($code);
	}
	
	/** 
	* Reply to talk
	* @http_method post get put
	*/
	public function reply()
	{
		$code = 202;
		$errors = array();
		try{
			$message = $this->getReplyMessage($errors);
			if(empty($errors))
			{
				$id = $this->_messageMapper->save($message);
			}
		}catch(Exception $e)
		{
			$errors[] = $e->getMessage();
		}
		if(!empty($errors))
		{
			$code = 400;
			$this->_response->errors = $errors;
		}
		$this->response($code);
	}
	/** 
	* Reply to talk
	* @http_method post get put
	*/
	public function all()
	{
		$code = 202;
		$errors = array();
		try{
			$user = $this->user($errors);
			$messageId = $this->talk_id($errors);
			if(empty($errors))
			{
				if($this->_messageMapper->isGuest($messageId, $user))
					$this->_response->messages = $this->_messageMapper->listMessages($messageId, $user);
				else
					$errors[] = "Forbidden"
			}
		}catch(Exception $e)
		{
			$errors[] = $e->getMessage();
		}
		if(!empty($errors))
		{
			$code = 400;
			$this->_response->errors = $errors;
		}
		$this->response($code);
	}
}